<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .form {
            width: 400px;
            margin: 50px auto;

        }

        .form .content {
            width: 100%;
            height: 100px;
            outline: none;
            resize: none;
        }

        .form .contentBox .btn {
            float: right;
        }

        .commentList {
            margin-top: 20px;
        }

        .commentList .comment {
            font-size: 13px;
            line-height: 30px;
            border: 1px dashed #333;
        }

        .commentList .comment {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <div class="form">
        <div class="contentBox clear">
            <textarea class="content" name="" id="" cols="30" rows="10"></textarea>
            <div><button class="btn">发送</button></div>
        </div>
        <ul class="commentList">
            <!-- <li class="comment">xxxx</li> -->
        </ul>
    </div>
</body>
<script>


    var contentInp = document.getElementsByClassName("content")[0];
    var commentList = document.getElementsByClassName("commentList")[0];
    var btn = document.getElementsByClassName("btn")[0];

    // 敏感词列表 
    var keyWordsList = ["NMD", "CNMD", "CNM", "SB", "菜B", "FUCK", "NC", "ZZ"];

    btn.onclick = function () {
        var content = contentInp.value;

        if (content) {  // 有内容在方式

            // 点击按钮  新增一个li 将评论的内容放到li中  -> 在将li放到页面中
            var li = document.createElement("li");
            li.className = "comment";

            // 检测是否存在敏感内容:   (评论中是否存在敏感词)   有敏感词警告
            var flag = false; //假设不存在

            for (var i = 0; i < keyWordsList.length; i++) {
                var key = keyWordsList[i]; // "NMD", "CNMD", "CNM"
                var reg = new RegExp(key, "i");   //检测是否存在

                if (reg.test(content)) {
                    flag = true;
                }

            }

            if (flag) {  // 有敏感词
                li.textContent = "系统检测您有不正当言论,将会对您开启言论检测; 发言条数:5/5";
                li.style.color = "red";
            } else {
                li.textContent = content;
            }
            // commentList.appendChild(li);
            commentList.prepend(li);
            contentInp.value = "";







        }

    }







</script>

</html>